/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.nail.zhaogr.service;

import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;

import com.nail.zhaogr.dao.MyCommendDao;
import com.nail.zhaogr.dao.MyFavoriteDao;
import com.nail.zhaogr.dao.PsnServiceAreaDao;
import com.nail.zhaogr.dao.PublishInfoDao;
import com.nail.zhaogr.model.MyCommend;
import com.nail.zhaogr.model.MyFavorite;
import com.nail.zhaogr.model.PsnServiceArea;
import com.nail.zhaogr.model.PublishInfo;
import com.nail.zhaogr.vo.query.MyCommendQuery;
import com.nail.zhaogr.vo.query.MyFavoriteQuery;
import com.nail.zhaogr.vo.query.PublishInfoQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PublishInfoManager extends BaseManager<PublishInfo,java.lang.Long>{

	private PublishInfoDao publishInfoDao;
	
	private PsnServiceAreaDao psnServiceAreaDao;
	
	private MyCommendDao myCommendDao;
	
	private MyFavoriteDao myFavoriteDao;
	
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setPublishInfoDao(PublishInfoDao dao) {
		this.publishInfoDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.publishInfoDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PublishInfoQuery query) {
		return publishInfoDao.findPage(query);
	}
	
	public void saveAllInfo(PublishInfo entity,List<PsnServiceArea> psnServiceAreas){
		publishInfoDao.save(entity);
		
		for (PsnServiceArea psnServiceArea:psnServiceAreas) {
			psnServiceArea.setPublishInfoId(entity.getPublishInfoId());
			psnServiceAreaDao.save(psnServiceArea);
		}
		
	}
	
	public String saveCommend(MyCommendQuery query){
		Page page = myCommendDao.findPage(query);
		if(page.getTotalCount()>0){
			return "您已经推荐过此工人信息";
		}else{
			MyCommend myCommend = new MyCommend();
			BeanUtils.copyProperties(query, myCommend);
			myCommendDao.save(myCommend);
			PublishInfo publishInfo = publishInfoDao.getById(query.getPublishInfoId());
			if(publishInfo.getCommendCount()!=null){
				publishInfo.setCommendCount(publishInfo.getCommendCount()+1);
			}else{
				publishInfo.setCommendCount(1);
			}
			publishInfoDao.update(publishInfo);
			return "推荐成功";
		}
	}
	
	public String saveFavorite(MyFavoriteQuery query){
		Page page = myFavoriteDao.findPage(query);
		if(page.getTotalCount()>0){
			return "您已经收藏过此工人信息";
		}else{
			MyFavorite myFavorite = new MyFavorite();
			BeanUtils.copyProperties(query, myFavorite);
			myFavoriteDao.save(myFavorite);
			PublishInfo publishInfo = publishInfoDao.getById(query.getPublishInfoId());
			if(publishInfo.getFavoriteCount()!=null){
				publishInfo.setFavoriteCount(publishInfo.getFavoriteCount()+1);
			}else{
				publishInfo.setFavoriteCount(1);
			}
			publishInfoDao.update(publishInfo);
			return "收藏成功";
		}
	}
	
	/*public List<PublishInfo> getExcellentWorker(){
		List<PublishInfo> list =null;
		list = publishInfoDao.fin
		return  list;
	}*/
	/**
	 * @param psnServiceAreaDao the psnServiceAreaDao to set
	 */
	public void setPsnServiceAreaDao(PsnServiceAreaDao psnServiceAreaDao) {
		this.psnServiceAreaDao = psnServiceAreaDao;
	}
	/**
	 * @param myCommendDao the myCommendDao to set
	 */
	public void setMyCommendDao(MyCommendDao myCommendDao) {
		this.myCommendDao = myCommendDao;
	}
	/**
	 * @param myFavoriteDao the myFavoriteDao to set
	 */
	public void setMyFavoriteDao(MyFavoriteDao myFavoriteDao) {
		this.myFavoriteDao = myFavoriteDao;
	}
	
}
